<template>
  <div>
    <!-- 面包屑 导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model="queryInfo.query" @clear="getGoodsList">
            <template #append>
              <el-button :icon="Search" @click="searchGoods" />
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="router.push('/goods/add')">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="goodsList" border stripe row-key="goods_id">
        <el-table-column type="index" width="80px"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="130px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150px">
          <template #default="{ row }">
            {{ formatDate(row.add_time) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template #default="{ row }">
            <el-button size="small" type="primary" :icon="Edit" @click="router.push(`/goods/${row.goods_id}`)"></el-button>
            <el-button size="small" type="danger" :icon="Delete" @click="delGoods(row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:current-page="queryInfo.pagenum"
        :page-sizes="pageSizes"
        v-model:page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { Search, Edit, Delete } from '@element-plus/icons-vue'
import { getGoodsListApi, deleteGoodsByIdApi } from '@/api/goods'
import { message, formatDate } from '@/utils/functions'
import { useRouter } from 'vue-router'
const router = useRouter()
// 分页大小
const pageSizes = ref([10, 20, 30, 40])
// 构建分页参数
const queryInfo = ref({
  query: '',
  pagenum: 1,
  pagesize: pageSizes.value[0]
})
// 获取商品列表数据
const goodsList = ref([])
// 获取总记录数
const total = ref(0)
const getGoodsList = async () => {
  const data = await getGoodsListApi(queryInfo.value)
  goodsList.value = data.goods
  total.value = data.total
  console.log(data)
}
getGoodsList()
// 商品搜索
const searchGoods = () => {
  queryInfo.value.pagenum = 1
  getGoodsList()
}

// 监听分页参数的变化
// 每页显示的条数发生改变时触发
const handleSizeChange = size => {
  queryInfo.value.pagesize = size
  getGoodsList()
}
// 当前页码改变时触发
const handleCurrentChange = page => {
  queryInfo.value.pagenum = page
  getGoodsList()
}
// 删除商品
const delGoods = async id => {
  try {
    // 弹框询问
    await ElMessageBox.confirm('您确定要删除该商品吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    // 确认删除
    await deleteGoodsByIdApi(id)
    message('success', '删除成功')
    getGoodsList()
  } catch {
    message('info', '已取消删除')
  }
}
</script>

<style lang="scss" scoped>
</style>